﻿using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArcEngine.CourseDesign.Win
{
    public partial class FrmAttrTable : UIForm
    {
        public FrmAttrTable()
        {
            InitializeComponent();
        }

        public IFeatureLayer CurrFeatureLayer { get; set; }

        public void InitUI()
        {
            if (null == this.CurrFeatureLayer) return;

            DataTable dt = new DataTable();
            for (int i = 0; i < this.CurrFeatureLayer.FeatureClass.Fields.FieldCount; i++)
            {
                DataColumn column = new DataColumn();
                IField field = this.CurrFeatureLayer.FeatureClass.Fields.get_Field(i);
                column.ColumnName = field.AliasName;// 获取字段名作为列标题
                column.DataType = Type.GetType("System.Object");// 定义列字段类型
                dt.Columns.Add(column); // 在数据表中添加字段信息
            }

            IFeatureCursor featureCursor = this.CurrFeatureLayer.Search(null, true);
            IFeature feature = featureCursor.NextFeature();
            while (null != feature)
            {
                DataRow row = dt.NewRow();
                // 获取字段属性
                for (int k = 0; k < dt.Columns.Count; k++)
                {
                    row[k] = feature.get_Value(k);
                }
                dt.Rows.Add(row);// 在数据表中添加字段属性信息
                feature = featureCursor.NextFeature();
            }
            Marshal.ReleaseComObject(featureCursor);// 释放指针
            this.dgv_AttrTable.DataSource = dt;
        }
    }
}
